﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using VSM;
namespace SAP
{
    public partial class P_WSC : System.Web.UI.Page
    {


        private static int GeneralID;
        private static string dbType;
        private static int WSCID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session[VSM.Utils.AppConstants.ID_User] == null
                          || Session[VSM.Utils.AppConstants.Password] == null
                          || Session[VSM.Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("../../Login.aspx");
            }
            if (!IsPostBack)
            {
                //dbType = Session[VSM.Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? VSM.Utils.AppConstants.VSM : VSM.Utils.AppConstants.DEALER;
                //GeneralID = Request.QueryString["GeneralID"] == null ? -1 : Int32.Parse(Request.QueryString["GeneralID"]);
                //int RequestType = Request.QueryString["requestType"] == null ? 0 : Int32.Parse(Request.QueryString["requestType"]);

                //loadWSC();

                //if (WSCID == -1)
                //{
                //    loadDefaultNew("MMBJNKB40FD000243");
                //}

                
                    /*
                Services sv = new Services();

                string sql = " select gs.JobID, gs.Receptionist, gs.AdviserCode, gs.TechnicianCode, jk.Name as JobKind, pm.Name as PaymentMethod, ";
                sql += " gs.DateIn, gs.TimeIn, gs.ExpDelivery, gs.ExpTime, gs.StartDate, gs.StartTime, ";
                sql += " gs.VehicleCode, gs.CustomerCode, ";
                sql += " adv.Name AdviserName, tec.Name TechnicianName, ";
                sql += " gs.FinishDate, gs.FinishTime, gs.ActualDelivery, gs.ActualDeliveryTime, gs.CustomerRequest, gs.Notice, ";
                sql += " gs.Odometer, gs.ServiceDate, ";
                sql += " sv.VIN, sv.Brand, sv.VehicleName, sv.EngineNo, sv.RegNo, sv.KeyNo, sv.ProductionDate, sv.PDIDate, ";
                sql += " sv.InvoiceDate, sv.Dealer, sv.ColorCode, sv.CustomerCode, sv.CustomerName, sv.LotNo, sv.Remarks, sv.Model, ";
                sql += " rd.CardName, rd.CardFName, rd.E_Mail, rd.Phone1, rd.Fax, rd.Cellular, rd.U_ContactPerson, rd.U_ContactPhone, ";
                sql += " rd.Address, rd.LicTradNum, rd.Notes, ";
                sql += " sd.ItemCode, sd.ItemName, sd.FrgnName, sd.Quantity, sd.Price, sd.VAT, sd.DiscPrcnt, sd.Stock ";
                sql += " from U_GeneralService gs join U_generalServiceDetail sd on gs.GeneralID = sd.GeneralID ";
                sql += " join VSM_ORG..OCRD rd on rd.CardCode = gs.CustomerCode ";
                sql += " join U_ServiceVehicle sv on sv.VehicleID = gs.VehicleCode ";
                sql += " left join VSM_ORG..[@JOBKINDSVC] jk on jk.Code = gs.JobKind";
                sql += " left join VSM_ORG..[@PAYMENTMETHODSVC] pm on pm.Code = gs.Payment ";
                sql += " left join [@TECHNICIAN] adv on gs.AdviserCode = adv.Code ";
                sql += " left join [@TECHNICIAN] tec on gs.TechnicianCode = tec.Code ";
                sql += " where gs.GeneralID = " + GeneralID.ToString();

                DataSet ds = sv.ExecuteQuery(dbType, sql);
                if (ds.Tables[0].Rows.Count > 0) {
                    DataRow dr = ds.Tables[0].Rows[0];
                    loadVehicle(dr["VehicleCode"].ToString());
                    loadCustomer(dr["CustomerCode"].ToString());

                    txt_Odometer.Text = dr["Odometer"].ToString();
                    txtDate.Text = FormatDate(dr["ServiceDate"].ToString());
                    //txt_PONo.Text = "PO-" + dr["JobID"].ToString();
                    //txt_Notice.Text = dr["Notice"].ToString();
                    //txt_DateIn.Text = FormatDate(dr["DateIn"].ToString());
                    //txt_TimeIn.Text = dr["TimeIn"].ToString();
                    //txt_ExpDate.Text = FormatDate(dr["ExpDelivery"].ToString());
                    //txt_ExpTime.Text = dr["ExpTime"].ToString();
                    //txt_StartDate.Text = FormatDate(dr["StartDate"].ToString());
                    //txt_StartTime.Text = dr["StartTime"].ToString();
                    //txt_FinishDate.Text = FormatDate(dr["FinishDate"].ToString());
                    //txt_FinishTime.Text = dr["FinishTime"].ToString();
                    //txt_ActualDate.Text = FormatDate(dr["ActualDelivery"].ToString());
                    //txt_ActualTime.Text = dr["ActualDeliveryTime"].ToString();
                    //txt_Receptionist.Text = dr["Receptionist"].ToString();

                    //txt_Technician.Text = dr["TechnicianName"].ToString();
                    //txt_Adviser.Text = dr["AdviserName"].ToString();

                    DataSet dsDetail = sv.ExecuteQuery(dbType, "select ROW_NUMBER() over (ORDER BY DetailCode) RowNum, * from [U_GeneralServiceDetail] where GeneralID = " + GeneralID.ToString());
                    grvPO.DataSource = dsDetail.Tables[0];
                    grvPO.DataBind();

                    string sqlLabour = " select ROW_NUMBER() over (ORDER BY LabourID) RowNum, PositionCode, FRTName, Quantity from dbo.U_GeneralServiceLabour gl join U_FRT frt on gl.FRTID = frt.FRTID ";
                    sqlLabour += " where GeneralID = " + GeneralID.ToString();
                    DataSet dsLabour = sv.ExecuteQuery(dbType, sqlLabour);
                    gv_Labour.DataSource = dsLabour.Tables[0];
                    gv_Labour.DataBind();
                }
*/
            }
        }

        private void loadWSC()
        {
           
            string sql = " SELECT [WSCID],gs.[GeneralID],[RegistrationNum],[DisCode],[DisName],wsc.[DealerCode],[DealerName]";
            sql += " ,[SeqNo],[InvoiceDate],[S],[ID],[T],[DIV],[ConditionCode],[A],[B],[C],[CustomerName],[VIN],[FailureDate],[KD],";
            sql += " [SoldDate],[HourMeter],[Manu],[RefCode],[M],[COM],[RatioL],[RatioP],[FCT],[Remarks],wsc.[Status], Model, EngineNumber, ";
            sql += " ServiceDate, Odometer";
            sql += "  FROM [U_WSC] wsc join U_GeneralService gs on wsc.GeneralID = gs.GeneralID";
            sql += "  where wsc.GeneralID = " + GeneralID.ToString();
            Services sv = new Services();
            DataSet ds = sv.ExecuteQuery("DEALER", sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                WSCID = Int32.Parse(dr["WSCID"].ToString());

                txtDate.Text =FormatDate(dr["ServiceDate"].ToString());
                txt_Odometer.Text = dr["Odometer"].ToString();

                txt_RegistrationNum.Text = dr["RegistrationNum"].ToString();
                txt_DistCode.Text = dr["DisCode"].ToString();
                txt_DistName.Text = dr["DisName"].ToString();
                txt_DealerCode.Text = dr["DealerCode"].ToString();
                txt_DealerName.Text = dr["DealerName"].ToString();
                txt_SeqNo.Text = dr["SeqNo"].ToString();
                txt_InvoiceDate.Text = FormatDate(dr["InvoiceDate"].ToString());
                txt_S.Text = dr["S"].ToString();
                txt_ID.Text = dr["ID"].ToString();
                txt_T.Text = dr["T"].ToString();
                txt_DIV.Text = dr["DIV"].ToString();

                txt_Model.Text = dr["Model"].ToString();
                txt_EngineNum.Text = dr["EngineNumber"].ToString();

                txt_ConditionCode.Text = dr["ConditionCode"].ToString();
                txt_A.Text = dr["A"].ToString();
                txt_B.Text = dr["B"].ToString();
                txt_C.Text = dr["C"].ToString();
                txt_CusName.Text = dr["CustomerName"].ToString();
                txt_VIN.Text = dr["VIN"].ToString();

                txt_FailureDate.Text = FormatDate(dr["FailureDate"].ToString());
                txt_KD.Text = dr["KD"].ToString();

                txt_SoldDate.Text = FormatDate(dr["SoldDate"].ToString());

                txt_HourMeter.Text = dr["HourMeter"].ToString();
                txt_Manu.Text = dr["Manu"].ToString();
                txt_RefCode.Text = dr["RefCode"].ToString();
                txt_M.Text = dr["M"].ToString();
                txt_COM.Text = dr["COM"].ToString();
                txt_RatioL.Text = dr["RatioL"].ToString();
                txt_RatioP.Text = dr["RatioP"].ToString();
                txt_FCT.Text = dr["FCT"].ToString();
                txt_Notice.Text = dr["Remarks"].ToString();
            }
        }

        private string FormatDate(string dateIn)
        {
            if (dateIn.Trim() == "")
                return "";
            DateTime pars = DateTime.Parse(dateIn);
            return pars.ToString("dd/MM/yyyy");

        }

        private void loadDefaultNew(string VIN)
        {
            //get dealer Name
            Services sv = new Services();
            string sqlDealer = "select CardCode, CardName from OCRD where CardCode = '" + Session[VSM.Utils.AppConstants.ID_Dealer] + "'";
            DataSet dsDealer = sv.ExecuteQuery("VSM", sqlDealer);
            if (dsDealer.Tables[0].Rows.Count > 0) {
                txt_DealerCode.Text = dsDealer.Tables[0].Rows[0]["CardCode"].ToString();
                txt_DealerName.Text = dsDealer.Tables[0].Rows[0]["CardName"].ToString();
            }

            string sql = " select inv.DocDate, rn.DistNumber as VIN, rn.Notes as EngineNo, ms.Name as ModelName, ";
            sql += " cg.RegistrationNumber, inv.CardCode, inv.CardName ";
            sql += " from OINV inv join INV1 inv1 on inv.DocEntry = inv1.BaseEntry ";
            sql += " join  OSRN rn on inv1.ItemCode = rn.ItemCode ";
            sql += " join OITM it on it.ItemCode = inv1.ItemCode ";
            sql += " join [@ModelSales] ms on ms.Code = it.U_ModelSales ";
            sql += " left join U_CustomerRegis cg on cg.VIN = rn.DistNumber ";
            sql += " where rn.DistNumber = '" + VIN + "'";
            
            DataSet dsInfo = sv.ExecuteQuery("VSM", sql);

            if (dsInfo.Tables[0].Rows.Count > 0) {
                txt_RegistrationNum.Text = dsInfo.Tables[0].Rows[0]["RegistrationNumber"].ToString();
                txt_InvoiceDate.Text = FormatDate(dsInfo.Tables[0].Rows[0]["DocDate"].ToString());
                txt_CusName.Text = dsInfo.Tables[0].Rows[0]["CardName"].ToString();
                txt_VIN.Text = dsInfo.Tables[0].Rows[0]["VIN"].ToString();
                txt_Model.Text = dsInfo.Tables[0].Rows[0]["ModelName"].ToString();
                txt_EngineNum.Text = dsInfo.Tables[0].Rows[0]["EngineNo"].ToString();
            }

            string sqlSeq = "select isnull(max(SeqNo), 0) + 1 NextNumber from U_WSC where DealerCode = '" + Session[VSM.Utils.AppConstants.ID_Dealer] + "'";
            DataSet dsSeq = sv.ExecuteQuery("DEALER", sqlSeq);
            txt_SeqNo.Text = dsSeq.Tables[0].Rows[0]["NextNumber"].ToString();

            string sqlOdo = "select Odometer, ServiceDate from U_GeneralService where GeneralID = " + GeneralID.ToString();
            DataSet dsOdo = sv.ExecuteQuery("DEALER", sqlOdo);
            if (dsOdo.Tables[0].Rows.Count > 0) {
                txt_Odometer.Text = dsOdo.Tables[0].Rows[0]["Odometer"].ToString();
                txtDate.Text = FormatDate(dsOdo.Tables[0].Rows[0]["ServiceDate"].ToString());
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("GeneralID");
            dt.Columns.Add("RegistrationNum");
            dt.Columns.Add("DisCode");
            dt.Columns.Add("DisName");
            dt.Columns.Add("DealerCode");
            dt.Columns.Add("DealerName");
            dt.Columns.Add("SeqNo");
            dt.Columns.Add("InvoiceDate");
            dt.Columns.Add("S");
            dt.Columns.Add("ID");
            dt.Columns.Add("T");
            dt.Columns.Add("DIV");
            dt.Columns.Add("ConditionCode");
            dt.Columns.Add("A");
            dt.Columns.Add("B");
            dt.Columns.Add("C");
            dt.Columns.Add("CustomerName");
            dt.Columns.Add("VIN");
            dt.Columns.Add("FailureDate");
            dt.Columns.Add("KD");
            dt.Columns.Add("SoldDate");
            dt.Columns.Add("HourMeter");
            dt.Columns.Add("Manu");
            dt.Columns.Add("RefCode");
            dt.Columns.Add("M");
            dt.Columns.Add("COM");
            dt.Columns.Add("RatioL");
            dt.Columns.Add("RatioP");
            dt.Columns.Add("FCT");
            dt.Columns.Add("Remarks");
            dt.Columns.Add("Status");
            dt.Columns.Add("WSCID");
            dt.Columns.Add("Model");
            dt.Columns.Add("EngineNumber");

            DataRow dr = dt.NewRow();
            dr["GeneralID"] = GeneralID;
            dr["RegistrationNum"] = txt_RegistrationNum.Text;
            dr["DisCode"] = txt_DistCode.Text;
            dr["DisName"] = txt_DistName.Text;
            dr["DealerCode"] = txt_DealerCode.Text;
            dr["DealerName"] = txt_DealerName.Text;
            dr["SeqNo"] = txt_SeqNo.Text;

            if (txt_InvoiceDate.Text.Trim() == "")
                dr["InvoiceDate"] = DBNull.Value;
            else
                dr["InvoiceDate"] = VSM.Utils.AppConstants.ConvertToDate(txt_InvoiceDate.Text.Trim());

            dr["S"] = txt_S.Text;
            dr["ID"] = txt_ID.Text;
            dr["T"] = txt_T.Text;
            dr["DIV"] = txt_DIV.Text;

            dr["ConditionCode"] = txt_ConditionCode.Text;
            dr["A"] = txt_A.Text;
            dr["B"] = txt_B.Text;
            dr["C"] = txt_C.Text;
            dr["CustomerName"] = txt_CusName.Text;
            dr["VIN"] = txt_VIN.Text;

            if (txt_FailureDate.Text.Trim() == "")
                dr["FailureDate"] = DBNull.Value;
            else
                dr["FailureDate"] = VSM.Utils.AppConstants.ConvertToDate(txt_FailureDate.Text.Trim());

            dr["KD"] = txt_KD.Text;

            if (txt_SoldDate.Text.Trim() == "")
                dr["SoldDate"] = DBNull.Value;
            else
                dr["SoldDate"] = VSM.Utils.AppConstants.ConvertToDate(txt_SoldDate.Text.Trim());

            dr["HourMeter"] = txt_HourMeter.Text;
            dr["Manu"] = txt_Manu.Text;
            dr["RefCode"] = txt_RefCode.Text;
            dr["M"] = txt_M.Text;
            dr["COM"] = txt_COM.Text;
            dr["RatioL"] = txt_RatioL.Text;
            dr["RatioP"] = txt_RatioP.Text;
            dr["FCT"] = txt_FCT.Text;
            dr["Remarks"] = txt_Notice.Text;
            dr["Status"] = 0;
            dr["WSCID"] = WSCID;

            dr["Model"] = txt_Model.Text;
            dr["EngineNumber"] = txt_EngineNum.Text;

            dt.Rows.Add(dr);

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            Services sv = new Services();
            if (WSCID == -1)
            {
                WSCID = sv.InsertWSC(dbType, ds);
                if (WSCID != -1)
                {
                    lb_Message.Text = "[Add New] Operation complete.";
                }
                else
                    lb_Message.Text = "[Add New] Operation failure.";
            }
            else {
                int count = sv.UpdateWSC(dbType, ds);
                if (count != -1)
                    lb_Message.Text = "[Update] Operation complete.";
                else
                    lb_Message.Text = "[Update] Operation failure.";
            }
        }

        protected void btn_Print_Click(object sender, EventArgs e)
        {

        }
    }
}